<template>
  <div style="width: 80%;">
      <div style="margin-bottom: 30px">编辑图书</div>
    <el-form :inline="true"   :model="form" :rules="rules" ref="ruleForm"  style="width: 70%" label-width="200px">
      <el-form-item label="图书标准码" prop="bookNo">
        <el-select v-model="form.bookNo" clearable filterable placeholder="请选择" @change="selBook">
          <el-option
              v-for="item in books"
              :key="item.id"
              :label="item.bookNo"
              :value="item.bookNo">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="图书名称" prop="bookName">
        <el-input v-model="form.bookName" disabled placeholder="请输入名称"></el-input>
      </el-form-item>

      <el-form-item label="所需积分" prop="score">
        <el-input v-model="form.score" disabled ></el-input>
      </el-form-item>



      <el-form-item label="用户会员码" prop="userNo">
        <el-select v-model="form.userNo" clearable filterable placeholder="请选择" @change="selUser">
          <el-option
              v-for="item in users"
              :key="item.id"
              :label="item.username"
              :value="item.username">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="用户名称" prop="userName">
        <el-input disabled v-model="form.userName"></el-input>
      </el-form-item>
      <el-form-item label="用户联系方式" prop="userPhone">
        <el-input disabled v-model="form.userPhone" ></el-input>
      </el-form-item>



    </el-form>

    <div style="text-align: center;margin-top: 30px ">
      <el-button type="primary"  @click="save" size="medium">提交</el-button>
<!--      <el-button type="danger">返回列表</el-button>-->
    </div>

  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  name: 'EditCategory',
  data(){
    return{
      form:{

      },
      books:[],
      users:[],
      rules: {
        bookNo: [
          {required: true, message: '请输入图书标准码', trigger: 'blur'}
        ],
        userNo: [
          {required: true, message: '请输入会员码', trigger: 'blur'}
        ],
      }
    }

  },
  created() {
    request.get('/book/list',this.form).then(res =>{
      this.books = res.data
    })

    request.get('/user/list',this.form).then(res =>{
      this.users = res.data
    })

    const  id = this.$route.query.id
    request.get("/borrow/"+id).then(res =>{
      this.form = res.data
    })

  },
  methods:{
    save(){
      request.put('/borrow/update',this.form).then(res =>{
        if (res.code === '200' ){
          this.$notify.success('更新成功')
          this.$router.push("/bookList")
        }else {
          this.$notify.error(res.msg)
        }
      })
    },
    selBook(){
      const  book = this.books.find(v => v.bookNo === this.form.bookNo)
      console.log(book)
      this.form.bookName = book.name
      this.form.score = book.score

    },
    selUser(){
      const  user = this.users.find(v => v.username === this.form.userNo)
      console.log(user)
      this.form.userName = user.name
      this.form.userPhone = user.phone

    }
  }
}
</script>